package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wit.dao.InsurancedefDao;
import com.wit.po.Insurancedef;

public class InsurancedefDaoImpl  implements InsurancedefDao {

	public Serializable addInsurancedef(Insurancedef insurancedef) {
		Session s = null;
		Transaction tx = null;
		Serializable serial = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			serial = s.save(insurancedef);
			tx.commit();
			s.close();
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return serial;
	}

	public boolean deleteInsurancedef(Insurancedef insurancedef) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(insurancedef);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}


	public List<Insurancedef> findAllInsurancedef() {
		Session s = null;
		List<Insurancedef> compList = new ArrayList<Insurancedef>();
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Insurancedef>)s.createQuery("FROM Insurancedef").list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return compList;
	}

	
	public Insurancedef findInsurancedefByIdenfier(Integer insuranceId) {
		Session s = null;
		Insurancedef insurancedef = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Insurancedef> compList = (List<Insurancedef>)s.createQuery("FROM Insurancedef WHERE insuranceId = ?").setInteger(0, insuranceId).list();
			if(compList.size() == 1)
				insurancedef = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return insurancedef;
	}


	public boolean modifyInsurancedef(Insurancedef insurancedef) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(insurancedef);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	public Insurancedef findInsurancedefBySubcompInsurance(
			String subcomp, String insuranceNumber) {
		Session s = null;
		List<Insurancedef> insurancedefList = null;
		Insurancedef comp = null;
		System.out.println("subcompanyId:"+subcomp+"*********insuranceNumber:"+insuranceNumber);
		try{
			s = HibernateSessionFactory.getSession();
			insurancedefList = (List<Insurancedef>)s.createQuery("FROM Insurancedef WHERE insurancetype.insuranceNumber=? AND subcompany.subcommpanyId=?").setString(0, insuranceNumber).setString(1, subcomp).list();
			if(insurancedefList.size() == 1)
				comp = insurancedefList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		System.out.println("insuranceId**********"+comp.getInsuranceId());
		return comp;
	}
}
